package doa;

import java.util.ArrayList;
import java.util.List;
import models.Brand;
import models.Person;
import org.hibernate.Session;
import org.hibernate.Transaction;
import service.HibernateUtil;

/**
 *
 * @author User
 */
public class BrandDAO {

    public BrandDAO() {
    }
    
    
    public static List<Brand> getAllBrands() {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Brand> brand_list = session.createCriteria(Brand.class).list();
        tx.commit();

        return brand_list;
    }
    
     public static Brand getBrand(int Brand_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Brand brand = (Brand) session.get(Brand.class, Brand_id);
        tx.commit();
        return brand;
    }
    
    public static ArrayList insertBrand(Brand brand) {
        ArrayList validation_errors = new ArrayList();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        session.save(brand);
        tx.commit();
        return validation_errors;
    }
    
    public static ArrayList editBrand(Brand brand) {
        ArrayList validation_errors = new ArrayList();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        session.update(brand);
        tx.commit();
        return validation_errors;
    }
    
    
    public static void deleteBrand(int Brand_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Brand brand = (Brand) session.load(Brand.class, Brand_id);
        session.delete(brand);
        tx.commit();
    }
//    private static Person getNewsFromRequest(HttpServletRequest request, Course course) {
//        Person p = new Person();
//
//        if (request.getParameter("newsid") != null && !request.getParameter("newsid").isEmpty()) {
//            p.setNewsid(Integer.parseInt(request.getParameter("newsid")));
//        }
//        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
//        Date date = new Date();
//        p.setNewsdate(dateFormat.format(date));
//        if (request.getParameter("newstitle") != null && !request.getParameter("newstitle").isEmpty()) {
//            p.setNewstitle(request.getParameter("newstitle"));
//        }
//        if (request.getParameter("newscontent") != null && !request.getParameter("newscontent").isEmpty()) {
//            p.setNewscontent(request.getParameter("newscontent"));
//        }
//        if (request.getParameter("newsglobal") != null && !request.getParameter("newsglobal").isEmpty()) {
//            p.setNewsglobal(Integer.parseInt(request.getParameter("newsglobal")));
//        }
//        if (course != null) {
//            n.setCourse(course);
//        }
//        return n;
//    }
    
    
    
}
